<?php
include_once("../../untils/conn.php");// 引入数据库连接配置文件

// 腾讯云短信发送 API 接口
function sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign) {
    $timestamp= $_POST['time'] ;
    $time = date('Y-m-d H:i:s', $timestamp);

    // 短信模板中的变量及值，按照模板格式填写
    $params = array(
        '{1}' => $_POST['oderid'],
        '{2}' => $time,
    );

    // 生成当前时间戳
    $currentTimeStamp = time();
    $random = rand(1000000, 9999999); // 生成一个随机数

    // 按照腾讯云 API 要求构造请求参数
    $requestParms = array(
        'tel' => array(
            'nationcode' => '86',
            'mobile' => $phoneNumber,
        ),
        'sig' => hash("sha256", "appkey=$appKey&random=$random&time=$currentTimeStamp&mobile=$phoneNumber", false),
        'tpl_id' => $templateId,
        'params' => array_values($params),
        'sign' => $smsSign,
        'time' => $currentTimeStamp,
        'extend' => '',
        'ext' => ''
    );

    // 转为 JSON 格式
    $requestJson = json_encode($requestParms);

    // 发送请求
    $url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=$appId&random=$random";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestJson);
    $result = curl_exec($ch);

    // if (curl_errno($ch)) {
    //     echo 'Error:' . curl_error($ch);
    // } else {
    //     echo $result;
    // }
    // curl_close($ch);

    // return $result;
}

$id = '1';
$sql = "SELECT * FROM sms WHERE id =".$id;
    $result = mysqli_query($con, $sql);
    
    
     if ($result->num_rows > 0) {
      // 输出数据
      while($row = $result->fetch_assoc()) {
        // 获取每个字段的值
        $appId = $row["appid"];
        $appKey = $row["appkey"];
        $phoneNumber = $row["admin_phone"];
        $templateId = $row["te_id"];
        $smsSign = $row["sign"];
        
      }
    } else {
    //   echo "未找到匹配的记录";
    }
    
// 调用函数发送短信
// sendSms($appId, $appKey, $phoneNumber, $templateId, $smsSign);
